package com.itheima.web.controller.store;


import com.github.pagehelper.PageInfo;
import com.itheima.domain.store.QuestionItem;
import com.itheima.utils.BeanUtil;
import com.itheima.web.controller.BaseServlet;
import org.apache.commons.lang3.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/store/questionItem")
public class QuestionItemServlet extends BaseServlet {
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String operation = req.getParameter("operation");
        if ("list".equals(operation)) {
            list(req, resp);
        } else if ("save".equals(operation)) {
            save(req, resp);
        } else if ("delete".equals(operation)) {
            delete(req, resp);
        } else if ("toEdit".equals(operation)) {
            toEdit(req, resp);
        } else if ("edit".equals(operation)) {
            update(req, resp);
        }else if ("saveOrUpdate".equals(operation)) {
            saveOrUpdate(req, resp);
        }
    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String questionId = req.getParameter("questionId");
        req.setAttribute("questionId",questionId);


        PageInfo all = questionItemService.findAll(questionId,1, 100);
        req.setAttribute("page", all);

        req.getRequestDispatcher("/WEB-INF/pages/store/questionItem/list.jsp").forward(req, resp);

    }


    private void saveOrUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        QuestionItem questionItem =  BeanUtil.fillBean(req,QuestionItem.class,"yyyy-MM-dd");
        if(StringUtils.isNoneBlank(questionItem.getId())){
            questionItemService.update(questionItem);
        }else{
            questionItemService.save(questionItem);
        }
        list(req,resp);

    }    private void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        QuestionItem questionItem = BeanUtil.fillBean(req, QuestionItem.class, "yyyyy-MM-dd");

        questionItemService.save(questionItem);
        list(req,resp);

    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        QuestionItem questionItem = BeanUtil.fillBean(req, QuestionItem.class);
        questionItemService.delete(questionItem);
        list(req,resp);
    }

    private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        QuestionItem questionItem = questionItemService.findById(id);
        req.setAttribute("questionItem", questionItem);
        list(req,resp);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        QuestionItem questionItem =  BeanUtil.fillBean(req,QuestionItem.class,"yyyy-MM-dd");
        questionItemService.update(questionItem);
        resp.sendRedirect(req.getContextPath()+"/store/questionItem?operation=list");
    }


    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }
}
